<?php
class XpageController extends Zend_Controller_Action
{
	private $_uiVar = array();
	private $_page;

	public function init()
	{
		$this->_uiVar["html"]["title"] = STE_Html::WEBSITE_TITLE;
		try
		{
			$db = Zend_Registry::get("DB");
			$this->_page = new STE_Page($db);
		}
		catch (Exception $ex)
		{
			echo $ex->getMessage();
		}
	}

	function indexAction()
	{
		$this->_redirect("/xpage/list");
	}

	public function listAction()
	{
		$adminSession = new Zend_Session_Namespace("STE_ADMIN");
		if ((isset($adminSession->member["IS_LOGIN"])) && ($adminSession->member["IS_LOGIN"] == 1))
		{
			$smarty = Zend_Registry::get("SMARTY");
			$smarty->assign("uiVar", $this->_uiVar);
			$smarty->display("x/page/index.tpl");
		} else {
			$this->_redirect('/xmember/login');
		}
	}
	
	public function getPageListAction()
	{
		$adminSession = new Zend_Session_Namespace("STE_ADMIN");
		if ((isset($adminSession->member["IS_LOGIN"])) && ($adminSession->member["IS_LOGIN"] == 1))
		{
			try
			{
				$pages = $this->_page->getPageList();
				echo json_encode(array("code" => 0, "data" => $pages));
			}
			catch (Exception $ex)
			{
				echo json_encode(array("code" => -101, "data" => $ex->getMessage()));
			}
		} else {
			echo json_encode(array("code" => -100, "data" => "- Bạn cần phải đăng nhập để thực hiện chức năng này. <br />"));
		}
	}

	function infoAction()
	{
		$adminSession = new Zend_Session_Namespace("STE_ADMIN");
		if ((isset($adminSession->member["IS_LOGIN"])) && ($adminSession->member["IS_LOGIN"] == 1))
		{
			try
			{
				$pageID = (int)trim($this->_getParam("id"));
				$languageID = trim($this->_getParam("lang"));
				$page = $this->_page->getPage($pageID, $languageID);
				echo json_encode(array("code" => 0, "data" => $page));
			}
			catch (Exception $ex)
			{
				echo json_encode(array("code" => -101, "data" => $ex->getMessage()));
			}
		}
		else
			echo json_encode(array("code" => -100, "data" => "- Bạn cần phải đăng nhập để thực hiện chức năng này. <br />"));
	}

	function editAction()
	{
		$adminSession = new Zend_Session_Namespace("STE_ADMIN");
		if ((isset($adminSession->member["IS_LOGIN"])) && ($adminSession->member["IS_LOGIN"] == 1))
		{
			try
			{
				$formData = $_POST;
				$formData = $this->_trimEditPageData($formData);
				$errorMessage = $this->_checkEditPageData($formData);
				if (empty($errorMessage))
				{
					$row = $this->_page->editPage($formData["editPageID"], $formData["editPageLanguageID"], $formData["editPageContent"]);
					echo json_encode(array("code" => 0, "data" => "- Thành công. <br />"));
				}
				else
					echo json_encode(array("code" => -99, "data" => $errorMessage));
			}
			catch (Exception $ex)
			{
				echo json_encode(array("code" => -101, "data" => $ex->getMessage()));
			}
		}
	}

	private function _trimEditPageData($formData)
	{
		$formData["editPageID"] = (int)trim($formData["editPageID"]);
		$formData["editPageLanguageID"] = (int)trim($formData["editPageLanguageID"]);
		$formData["editPageContent"] = trim($formData["editPageContent"]);
		return $formData;
	}

	private function _checkEditPageData($formData)
	{
		$errorMessage = "";
		if (empty($formData["editPageID"]) || empty($formData["editPageLanguageID"]))
			$errorMessage = "- Vui lòng nhập vào các thông tin bắt buộc. <br />";
		return $errorMessage;
	}
}
?>